package com.rs.controller;

import java.awt.EventQueue;
import java.util.List;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.github.pagehelper.Page;
import com.rs.model.ZhItemsRecipients;
import com.rs.model.ZhOvertime;
import com.rs.model.ZhUser;
import com.rs.service.AuditInformationService;
import com.rs.util.DateFormatUtil;
import com.rs.util.SpringContextUtils;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;

public class SelectItemsRecipients extends JInternalFrame {
	private JTable table;
	private ZhUser user;
	
	SelectItemsRecipients it=this;
	
	JComboBox comboBox_state ;
	
	private int pageNum=1;//页码
	private int pages;//总也数
	private int state=1;//状态 1为待审核 2为已驳回 3为通过
	
	
	private AuditInformationService audit = SpringContextUtils.getBean(AuditInformationService.class);

	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectItemsRecipients frame = new SelectItemsRecipients();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SelectItemsRecipients() {
		
		aa();
	}
	
	public SelectItemsRecipients(ZhUser user) {
		this.user=user;
		aa();
	}
	
	void aa(){
		setBounds(100, 100, 639, 459);
		setMaximizable(true);
		setIconifiable(true);
		setClosable(true);
		getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(25, 105, 557, 227);
		getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setRowHeight(20);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
			},
			new String[] {
				"id", "姓名", "物品名称", "领用数量", "领用时间"
			}
		));
		scrollPane.setViewportView(table);
		
		// Step1 添加行点击绑定事件 
		  table.addMouseListener(new MouseAdapter() { 
		   @Override 
		   public void mouseClicked(MouseEvent e) {                 
			   rowClicked();   
		    }
		   /** *行点击事件 * 
		   *<br>-----------------------------------------------------<br> *@author flotage 
		   *@create 2013-4-17上午10:09:14 *@note */ 
		   private void rowClicked() {
		    // 获取被点击的行 
		    int row = table.getSelectedRow(); 
		    // 获取点击行的内容 
          DefaultTableModel dtm = (DefaultTableModel) table.getModel();         
          Integer id  =  (Integer)dtm.getValueAt(row, 0);     
          if(id==null){
        	  return;
          }
          ItemsRecipientsAudit uu = new ItemsRecipientsAudit(id,user,it);
          uu.setVisible(true);
		   }         
		  });  
		
		
		JButton button_upPage = new JButton("上一页");
		button_upPage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				upPage();
			}
		});
		button_upPage.setBounds(353, 357, 93, 23);
		getContentPane().add(button_upPage);
		
		JButton button_nextPage = new JButton("下一页");
		button_nextPage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nextPage();
			}
		});
		button_nextPage.setBounds(456, 357, 93, 23);
		getContentPane().add(button_nextPage);
		
		comboBox_state = new JComboBox();
		comboBox_state.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str = comboBox_state.getSelectedItem().toString();
				if(str.equals("待审核")){
					state = 1;
				}else if(str.equals("已驳回")){
					state = 2;
				}else if(str.equals("已通过")){
					state = 3;
				}
				pageNum=1;
				nextPage();
			}
		});
		comboBox_state.setModel(new DefaultComboBoxModel(new String[] {"待审核", "已驳回", "已通过"}));
		comboBox_state.setBounds(25, 64, 93, 21);
		getContentPane().add(comboBox_state);
		
		JButton button = new JButton("刷新");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				shuaxin();
			}
		});
		button.setBounds(489, 63, 74, 23);
		getContentPane().add(button);
		
		nextPage();
		
	}
	
	
	private List<ZhItemsRecipients> getItemsRecipients(){
		ZhItemsRecipients it= new ZhItemsRecipients();
		if(state ==1){
			it.setIt_audit(0);
		}else if(state ==2){
			it.setIt_audit(2);
		}else if(state ==3){
			it.setIt_audit(1);
		}
		it.setPageNum(pageNum);
		Page<ZhItemsRecipients> page = audit.selectItemsRecipientsAudit(it);
		pages=page.getPages();
		return page;
	}
	
	//数据渲染
	private void  setTheValue(List<ZhItemsRecipients> list){
		TableModel ta = table.getModel();
		for(int i=0;i<ta.getRowCount();i++){
			if(i>=list.size()){
				ta.setValueAt(null, i, 0);
				ta.setValueAt(null, i, 1);
				ta.setValueAt(null, i, 2);
				ta.setValueAt(null, i, 3);
				ta.setValueAt(null, i, 4);
				continue;
			}
			ZhItemsRecipients ee = list.get(i);
			ta.setValueAt(ee.getIt_id(), i, 0);
			ta.setValueAt(ee.getItUname(), i, 1);
			ta.setValueAt(ee.getIt_name(), i, 2);
			ta.setValueAt(ee.getIt_num(), i, 3);
			ta.setValueAt(DateFormatUtil.getDateStringw(ee.getIt_time(),"yyyy.MM.dd HH:mm"), i, 4);
		}
	}
	
	//上一页
		private void upPage(){
			if(pageNum<=2){
				alert("当前是第一页！！");
				return;
			}
			setTheValue(getItemsRecipients());
			pageNum--;
		}
		
		//下一页
		private void nextPage(){
			if(pageNum >=pages && pageNum !=1){
				alert("当前是最后一页！！");
				return;
			}
			setTheValue(getItemsRecipients());
			pageNum++;
		}
		//。弹窗
		private void alert(String aa){
			JOptionPane.showMessageDialog(this, aa);
		}
		//页面刷新
		private void shuaxin(){
			pageNum--;
				setTheValue(getItemsRecipients());
			pageNum++;
		}
		
		public Integer dynamicRefresh(Integer id,Integer kk){
			DefaultTableModel ta = (DefaultTableModel)table.getModel();
			String str = "";
			ZhItemsRecipients it = audit.getIdZhItemsRecipients(id);
			if(it.getIt_audit() ==0){
				str="待审核";
			}else if(it.getIt_audit() ==1){
				str="已通过";
			}else if(it.getIt_audit() ==2){
				str="已驳回";
			}
			if(str.equals(comboBox_state.getSelectedItem())){
				for(int i=0;i<table.getRowCount();i++){
					Integer  tid = (Integer)ta.getValueAt(i, 0);
					if(it.getIt_id()==tid){
						return kk;
					}
					if(kk !=null){
						if(tid !=null  && it.getIt_id()<tid){
							continue;
						}
						ta.removeRow(table.getRowCount()-1);
						ta.insertRow(i, new Object[]{
								it.getIt_id(),
								it.getItUname(),
								it.getIt_name(),
								it.getIt_num(),
								DateFormatUtil.getDateStringw(it.getIt_time(),"yyyy.MM.dd HH:mm")
						});
						return kk;
					}
				}
					return kk;
			}else{
				for(int i=0;i<table.getRowCount();i++){
					Integer  tid = (Integer)ta.getValueAt(i, 0);
					if(tid !=null && tid==id){
						kk=i;
						ta.removeRow(i);
						ta.addRow(new String[]{null, null, null, null, null});
						break;
					}
				}
				return kk;
			}
				
		}
		
		//动态刷新
		/*public void dynamicRefresh(Integer id){
			DefaultTableModel ta = (DefaultTableModel)table.getModel();
			for(int i=0;i<table.getRowCount();i++){
				int  tid = (int)ta.getValueAt(i, 0);
				if(tid==id){
					ta.removeRow(i);
					break;
				}
			}
			ta.addRow(new String[]{null, null, null, null, null});	
		}*/

}
